import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'IPTVOPlatform.settings')
django.setup()
import subprocess
from utils.encryption import cal_md5_string, cal_sha1_string, str_to_hex
from oplatform.models import MachineCodeData


def machine_code_auth(func):
    def auth():
        args = "dmesg | grep TBS | awk '/address/{print $8}'"
        p = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE)
        out = p.stdout.readlines()
        result = ''
        for line in out:
            result += str(line, encoding='utf-8')
        machine_code_hex = str_to_hex(result)
        machine_code_hex_auth = machine_code_hex.replace(' ', '#&#')
        ac_register_code = cal_sha1_string(cal_md5_string(machine_code_hex_auth))
        print(ac_register_code)
        codes = MachineCodeData.objects.all()
        for code in codes:
            print(code.code)
            if ac_register_code == code.code:
                return func()
    return auth
